package routers

import (
	"github.com/gin-gonic/gin"
	v1 "myblog/api/v1"
	"myblog/middleware"
	"myblog/utils"
)

func InitRouter() {
	gin.SetMode(utils.AppMode)
	r := gin.New()
	r.Use(middleware.Logger())
	r.Use(middleware.Cors())
	r.Use(gin.Recovery())

	// auth 是需要jwt
	auth := r.Group("/api/v1")
	auth.Use(middleware.JwtToken())
	{
		// User模块的路由接口
		auth.PUT("/user/:id", v1.EditUser)
		auth.DELETE("/user/:id", v1.DeleteUser)
		// Article模块的路由接口
		auth.POST("/article/add", v1.AddArticle)

		auth.PUT("/article/:id", v1.EditArticle)
		auth.DELETE("/article/:id", v1.DeleteArticle)
		// Category模块的路由接口
		auth.POST("/category/add", v1.AddCategory)

		auth.PUT("/category/:id", v1.EditCategory)
		auth.DELETE("/category/:id", v1.DeleteCategory)

		auth.POST("/upload", v1.UpLoad)
	}
	router := r.Group("/api/v1")
	{
		router.GET("/users", v1.GetUsers)
		router.POST("/user/add", v1.AddUser)

		router.GET("/articles", v1.GetArticles)
		router.GET("/article/:id", v1.GetArticleInfo)
		router.GET("/category_articles/:cid", v1.GetCategoryArticle)

		router.GET("/categorys", v1.GetCategorys)

		router.POST("/login", v1.Login)
	}

	_ = r.Run(utils.HttpPort)
}
